package com.lemon.smartsafetynew.ui.activity.patrol;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.event.common.TypeEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemListVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskVo;
import com.lemon.smartsafetynew.ui.adapter.patrol.PatrolResultAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/9/19.
 */

public class SafePatrolResultActivity extends SwipeBackActivity {

    @Bind(R.id.patrol_task_name)
    TextView patrolTaskName;

    @Bind(R.id.patrol_person_name)
    TextView patrolPersonName;

    @Bind(R.id.patrol_date)
    TextView patrolDate;

    @Bind(R.id.patrol_expect_time)
    TextView patrolExpectTime;

    @Bind(R.id.patrol_real_time)
    TextView patrolRealTime;

    @Bind(R.id.patrol_during_time)
    TextView patrolDuringTime;

    @Bind(R.id.safe_patrol_error_label)
    TextView safePatrolErrorLabel;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private static SafePatrolTaskVo safePatrolTaskVo;
    private static SafePatrolTaskItemListVo safePatrolTaskItemListVo;
    private static int totalTime;

    private List<SafePatrolTaskItemVo> safePatrolTaskItemVos;

    private PatrolResultAdapter patrolResultAdapter;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            safePatrolTaskVo = (SafePatrolTaskVo) bundle.get(KeyConstants.KEY_SAFE_PATROL_TASK);
            safePatrolTaskItemListVo = (SafePatrolTaskItemListVo) bundle.get(KeyConstants.KEY_SAFE_PATROL_TASK_CHECK_ITEM_LIST);
            totalTime = bundle.getInt(KeyConstants.KEY_SAFE_PATROL_TASK_DURING_TIME);
            safePatrolTaskItemVos = safePatrolTaskItemListVo.getSafePatrolTaskItemVos();
        } else {
            callFinish();
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle("本次巡查结果");
    }

    @Override
    protected void callFinish() {
        AppService.getBus().post(new TypeEvent()); //call页面刷新
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_patrol_result;
    }

    @Override
    protected void initViews() {
        super.initViews();

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        initCommon();
        initRecyclerView();
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {

        LinearLayoutManager manager = new LinearLayoutManager(SafePatrolResultActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setNestedScrollingEnabled(false);
        patrolResultAdapter = new PatrolResultAdapter(safePatrolTaskItemVos);
        recyclerView.setAdapter(patrolResultAdapter);
    }

    /**
     * 初始化列表
     */
    private void initCommon() {

        patrolTaskName.setText(CommonUtil.avoidNullString(safePatrolTaskVo.getF_name()));
        patrolPersonName.setText(CommonUtil.avoidNullString(safePatrolTaskVo.getUser_name()));
        patrolDate.setText(CommonUtil.avoidNullString(CommonUtil.secondToTimeStr(safePatrolTaskVo.getPlan_start_time(), DateTypeEnum.DAY)));
        patrolExpectTime.setText(CommonUtil.timeToStr2(safePatrolTaskVo.getPlan_start_time()) + " 至 "
                + CommonUtil.timeToStr2(safePatrolTaskVo.getPlan_end_time()));
        patrolRealTime.setText(CommonUtil.timeToStr2(System.currentTimeMillis() - totalTime * 1000) + " 至 "
                + CommonUtil.timeToStr2(System.currentTimeMillis()));

        patrolDuringTime.setText(CommonUtil.formatTime(totalTime));


        if(safePatrolTaskItemVos.isEmpty()) {      //为空时不显示
            safePatrolErrorLabel.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
        }
    }


    @OnClick(R.id.sure_btn)
    public void onViewClicked() {
        callFinish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
